const userGoodsDao = require('../../dao/userGoodsDao.js')

module.exports = async (obj, originalParam) => {
	let user_order_success = true;
	let {
		data = {}, verifyResult
	} = obj;
	let {
		db,
		_
	} = originalParam;
	let {
		out_trade_no,
		total_fee
	} = data;

	//设置订单支付成功
	let order = await db.collection('uni-id-base-order').doc(out_trade_no)
		.update({
			status: 2,
			transaction_id: data.payNo,
			paid_time: new Date().getTime(),
			update_time: new Date().getTime()
		});

	//----发货----
	let goods = await db.collection('order_goods').where({
		oid: out_trade_no
	}).get()

	let ids = []
	let userGoods = goods.data.map(v => {
		//后期可以再加入课程有效期 分销逻辑等
		let item = {
			user_id: v.user_id,
			goods_id: v.goods_id,
			goods_type: v.type,
			oid: v.oid,
		}
		ids.push(v.goods_id)
		return item
	})

	//课程订阅人数+1
	let addRes = await db.collection('course').where({
		_id: _.in(ids)
	}).update({
		"user_count": _.inc(1)
	})

	let res = await userGoodsDao.addAllUserGoods(userGoods)
	let accessToken
	// 获取accessToken
	await vk.callFunction({
		url: 'client/pub/index',
		title: '正在获取access_token', // loading的title
		success: (res) => {
			accessToken = res.data.token
		}
	})

	// 发送模板消息
	await vk.callFunction({
		url: 'client/pub/template',
		title: '发送模板消息', // loading的title
		data: {
			openId: 'osOLA6SEMWzs2TX2NdFxlEsLDcnk',
			accessToken: accessToken,
			template_id: 'ZR1hWTAUDqQyUYp5l_HrB8jmVjKySKgpGj9B8Haok_8',
			data: {
				thing2: {
					value: '测试名称'
				},
				thing7: {
					value: '测试姓名'
				},
				character_string6: {
					value: 'fhdjashfkjladhsjkfhakjlhf'
				},
				amount5: {
					value: '0.12'
				},
				time3: {
					value: '2019年12月11日 18:36'
				},
			}
		},
		success: (res) => {
			console.log(res)
		},
		error: (err) => {
			console.log(err)
		}
	})

	return user_order_success;
};